package com.ruoyi.web.controller.video;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.model.VsUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.video.VsDramasVideo;
import com.ruoyi.system.domain.video.VsDramasVideoEpisodes;
import com.ruoyi.system.service.video.IVsDramasVideoEpisodesService;
import com.ruoyi.system.service.video.IVsDramasVideoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.video.VsDramasVideoFavorite;
import com.ruoyi.system.service.video.IVsDramasVideoFavoriteService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
/**
 * This file is part of 南阳迈特网络科技有限公司.
 * @company  南阳迈特网络科技有限公司
 * @link     https://www.nymaite.com
 * @document https://product.nymaite.cn
 * @contact  service@nymaite.com
 * @author nymaite
 */
/**
 * 收藏点赞Controller
 * 
 * @author zzx
 * @date 2024-04-30
 */
@Api(tags = "收藏点赞")
@RestController
@RequestMapping("/video/favorite")
public class VsDramasVideoFavoriteController extends BaseController
{
    @Autowired
    private IVsDramasVideoFavoriteService vsDramasVideoFavoriteService;

    @Autowired
    private IVsDramasVideoEpisodesService vsDramasVideoEpisodesService;

    @Autowired
    private IVsDramasVideoService vsDramasVideoService;

    /**
     * 查询收藏点赞列表
     */
    @ApiOperation("查询收藏点赞列表")
    @GetMapping("/list")
    public TableDataInfo list(VsDramasVideoFavorite vsDramasVideoFavorite)
    {
        startPage();
        List<VsDramasVideoFavorite> list = vsDramasVideoFavoriteService.selectVsDramasVideoFavoriteList(vsDramasVideoFavorite);
        return getDataTable(list);
    }

    /**
     * 导出收藏点赞列表
     */
//    @ApiOperation("导出收藏点赞列表")
    @Log(title = "收藏点赞", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, VsDramasVideoFavorite vsDramasVideoFavorite)
    {
        List<VsDramasVideoFavorite> list = vsDramasVideoFavoriteService.selectVsDramasVideoFavoriteList(vsDramasVideoFavorite);
        ExcelUtil<VsDramasVideoFavorite> util = new ExcelUtil<VsDramasVideoFavorite>(VsDramasVideoFavorite.class);
        util.exportExcel(response, list, "收藏点赞数据");
    }

    /**
     * 获取收藏点赞详细信息
     */
//    @ApiOperation("获取收藏点赞详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return success(vsDramasVideoFavoriteService.selectVsDramasVideoFavoriteById(id));
    }

    /**
     * 新增收藏点赞
     */
    @ApiOperation("新增收藏点赞")
    @Log(title = "收藏点赞", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody VsDramasVideoFavorite vsDramasVideoFavorite)
    {
        // 新增成功后返回的是id，id肯定不会=0 如果=0就代表插入失败，直接通知前端500
        if ("favorite".equals(vsDramasVideoFavorite.getType())) {
            vsDramasVideoFavorite.setEpisodeId(null);
        }
        int i = vsDramasVideoFavoriteService.insertVsDramasVideoFavorite(vsDramasVideoFavorite);
        if (i > 0) {
            VsDramasVideoFavorite like = new VsDramasVideoFavorite();
            // 如果是点赞就查询目前这集视频真实点赞量是多少
            // 如果不是那就是收藏，就查询这部剧的真实收藏量
            if ("like".equals(vsDramasVideoFavorite.getType())) {
                like.setEpisodeId(vsDramasVideoFavorite.getEpisodeId());
            } else {
                like.setVid(vsDramasVideoFavorite.getVid());
            }
            like.setType(vsDramasVideoFavorite.getType());
            List<VsDramasVideoFavorite> list = vsDramasVideoFavoriteService.selectVsDramasVideoFavoriteList(like);
            // 根据type类型判断是点赞还是收藏，更新
            if ("like".equals(vsDramasVideoFavorite.getType())) {
                VsDramasVideoEpisodes update = new VsDramasVideoEpisodes();
                update.setId(vsDramasVideoFavorite.getId());
                update.setLikes(list.size());
                update.setUpdatetime(System.currentTimeMillis());
                vsDramasVideoEpisodesService.updateVsDramasVideoEpisodes(update);
                VsDramasVideoEpisodes episodes = vsDramasVideoEpisodesService.selectVsDramasVideoEpisodesById(Math.toIntExact(vsDramasVideoFavorite.getEpisodeId()));
                Integer num = list.size() + episodes.getFakeLikes();
                Map map = new HashMap<>();
                if (num > 1000) {
                    String s = (num / 1000) + "k";
                    map.put("sumLikes", s);
                    map.put("like", true);
                    return success(map);
                }
                map.put("sumLikes", num);
                map.put("like", true);
                return success(map);
            } else {
                VsDramasVideo video = new VsDramasVideo();
                video.setId(Math.toIntExact(vsDramasVideoFavorite.getVid()));
                video.setFavorites(list.size());
                video.setUpdatetime(System.currentTimeMillis());
                vsDramasVideoService.updateVsDramasVideo(video);
                VsDramasVideo dramasVideo = vsDramasVideoService.selectVsDramasVideoById(Math.toIntExact(vsDramasVideoFavorite.getVid()));
                Integer num = list.size() + dramasVideo.getFakeFavorites();
                Map map = new HashMap<>();
                if (num > 1000) {
                    String s = (num / 1000) + "k";
                    map.put("sumFavorites", s);
                    map.put("favorite", true);
                    return success(map);
                }
                map.put("sumFavorites", num);
                map.put("favorite", true);
                return success(map);
            }
        }
        return error("收藏点赞失败！");
    }

    /**
     * 修改收藏点赞
     */
//    @ApiOperation("修改收藏点赞")
    @Log(title = "收藏点赞", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody VsDramasVideoFavorite vsDramasVideoFavorite)
    {
        return toAjax(vsDramasVideoFavoriteService.updateVsDramasVideoFavorite(vsDramasVideoFavorite));
    }

    /**
     * 删除收藏点赞
     */
//    @ApiOperation("删除收藏点赞")
    @Log(title = "收藏点赞", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        return toAjax(vsDramasVideoFavoriteService.deleteVsDramasVideoFavoriteByIds(ids));
    }

    @Log(title = "收藏点赞", businessType = BusinessType.DELETE)
    @PostMapping("/delLikeOrFavorite")
    public AjaxResult cancelVideo(@RequestBody VsDramasVideoFavorite vsDramasVideoFavorite) {
        if (StringUtils.isEmpty(vsDramasVideoFavorite.getType())) {
            return error("类型为空！");
        }
        if (vsDramasVideoFavorite.getVid() == null && vsDramasVideoFavorite.getEpisodeId() == null) {
            return error("剧集信息都为空！");
        }
        try {
            VsUser user = SecurityUtils.getLoginUser().getVsUser();
            vsDramasVideoFavorite.setUserId(user.getId());
        } catch (Exception e) {
            return error(e.getMessage());
        }
        if ("favorite".equals(vsDramasVideoFavorite.getType())) {
            vsDramasVideoFavorite.setEpisodeId(null);
        }
        int i = vsDramasVideoFavoriteService.cancelVideo(vsDramasVideoFavorite);
        // 如果为0就代表没有被删除的数据
        if (i == 0) {
            return error();
        }
        VsDramasVideoFavorite like = new VsDramasVideoFavorite();
        // 如果是点赞就查询目前这集视频真实点赞量是多少
        // 如果不是那就是收藏，就查询这部剧的真实收藏量
        if ("like".equals(vsDramasVideoFavorite.getType())) {
            like.setEpisodeId(vsDramasVideoFavorite.getEpisodeId());
        } else {
            like.setVid(vsDramasVideoFavorite.getVid());
        }
        like.setType(vsDramasVideoFavorite.getType());
        List<VsDramasVideoFavorite> list = vsDramasVideoFavoriteService.selectVsDramasVideoFavoriteList(like);
        // 根据type类型判断是点赞还是收藏，更新
        if ("like".equals(vsDramasVideoFavorite.getType())) {
            VsDramasVideoEpisodes update = new VsDramasVideoEpisodes();
            update.setId(vsDramasVideoFavorite.getId());
            update.setLikes(list.size());
            update.setUpdatetime(System.currentTimeMillis());
            vsDramasVideoEpisodesService.updateVsDramasVideoEpisodes(update);
            VsDramasVideoEpisodes episodes = vsDramasVideoEpisodesService.selectVsDramasVideoEpisodesById(Math.toIntExact(vsDramasVideoFavorite.getEpisodeId()));
            Integer num = list.size() + episodes.getFakeLikes();
            Map map = new HashMap<>();
            if (num > 1000) {
                String s = (num / 1000) + "k";
                map.put("sumLikes", s);
                map.put("like", false);
                return success(map);
            }
            map.put("sumLikes", num);
            map.put("like", false);
            return success(map);
        } else {
            VsDramasVideo video = new VsDramasVideo();
            video.setId(Math.toIntExact(vsDramasVideoFavorite.getVid()));
            video.setFavorites(list.size());
            video.setUpdatetime(System.currentTimeMillis());
            vsDramasVideoService.updateVsDramasVideo(video);
            VsDramasVideo dramasVideo = vsDramasVideoService.selectVsDramasVideoById(Math.toIntExact(vsDramasVideoFavorite.getVid()));
            Integer num = list.size() + dramasVideo.getFakeFavorites();
            Map map = new HashMap<>();
            if (num > 1000) {
                String s = (num / 1000) + "k";
                map.put("sumFavorites", s);
                map.put("favorite", false);
                return success(map);
            }
            map.put("sumFavorites", num);
            map.put("favorite", false);
            return success(num);
        }
    }

}
